Expand description
2D geometry, with a focus on curves.
The kurbo library contains data structures and algorithms for curves and vector paths. It was designed to serve the needs of 2D graphics applications, but it is intended to be general enough to be useful for other applications. It can be used as “vocabulary types” for representing curves and paths, and also contains a number of computational geometry methods.
§Examples
Basic UI-style geometry:
use kurbo::{Insets, Point, Rect, Size, Vec2};
let pt = Point::new(10.0, 10.0);
let vector = Vec2::new(5.0, -5.0);
let pt2 = pt + vector;
assert_eq!(pt2, Point::new(15.0, 5.0));
let rect = Rect::from_points(pt, pt2);
assert_eq!(rect, Rect::from_origin_size((10.0, 5.0), (5.0, 5.0)));
let insets = Insets::uniform(1.0);
let inset_rect = rect - insets;
assert_eq!(inset_rect.size(), Size::new(3.0, 3.0));
Finding the closest position on a Shape
’s perimeter to a Point
:
use kurbo::{Circle, ParamCurve, ParamCurveNearest, Point, Shape};
const DESIRED_ACCURACY: f64 = 0.1;
/// Given a shape and a point, returns the closest position on the shape's
/// perimeter, or `None` if the shape is malformed.
fn closest_perimeter_point(shape: impl Shape, pt: Point) -> Option<Point> {
let mut best: Option<(Point, f64)> = None;
for segment in shape.path_segments(DESIRED_ACCURACY) {
let nearest = segment.nearest(pt, DESIRED_ACCURACY);
if best.map(|(_, best_d)| nearest.distance_sq < best_d).unwrap_or(true) {
best = Some((segment.eval(nearest.t), nearest.distance_sq))
}
}
best.map(|(point, _)| point)
}
let circle = Circle::new((5.0, 5.0), 5.0);
let hit_point = Point::new(5.0, -2.0);
let expectation = Point::new(5.0, 0.0);
let hit = closest_perimeter_point(circle, hit_point).unwrap();
assert!(hit.distance(expectation) <= DESIRED_ACCURACY);
§Features
This crate either uses the standard library or the libm
crate for
math functionality. The std
feature is enabled by default, but can be
disabled, as long as the libm
feature is enabled. This is useful for
no_std
environments. However, note that the libm
crate is not as
efficient as the standard library, and that this crate still uses the
alloc
crate regardless.
Re-exports§
pub use crate::arc::ArcAppendIter;
pub use crate::circle::CirclePathIter;
pub use crate::line::LinePathIter;
pub use crate::rect::RectPathIter;
pub use crate::rounded_rect::RoundedRectPathIter;
pub use crate::stroke::DashIterator;
Modules§
- Common mathematical operations
- Computation of offset curves of cubic Béziers, based on a curve fitting approach.
- Simplification of a Bézier path.
Structs§
- A 2D affine transform.
- A single elliptical arc segment.
- A Bézier path.
- A circle.
- A segment of a circle.
- A trivial “curve” that is just a constant.
- A single cubic Bézier segment.
- An iterator for cubic beziers.
- A sample point of a curve for fitting.
- An ellipse.
- Insets from the edges of a rectangle.
- A single line.
- The minimum distance between two Bézier curves.
- The nearest position on a curve to some point.
- An iterator for path segments.
- A 2D point.
- A single quadratic Bézier segment.
- An iterator for quadratic beziers.
- A quadratic Bézier spline in B-spline format.
- A rectangle.
- A rectangle with equally rounded corners.
- Radii for each corner of a rounded rectangle.
- An iterator that transforms path elements to path segments.
- A 2D size.
- Describes the visual style of a stroke.
- Options for path stroking.
- A single SVG arc segment.
- A transformation consisting of a uniform scaling followed by a translation.
- A 2D vector.
Enums§
- Defines the shape to be drawn at the ends of a stroke.
- Classification result for cusp detection.
- Defines the connection between two segments of a stroke.
- The element of a Bézier path.
- A segment of a Bézier path.
- Optimization level for computing
- An error which can be returned when parsing an SVG.
Constants§
- A default value for methods that take an ‘accuracy’ argument.
- The maximum number of extrema that can be reported in the
ParamCurveExtrema
trait.
Traits§
- A curve parametrized by a scalar.
- A parametrized curve that can have its arc length measured.
- A parametrized curve that can have its signed area measured.
- A parametrized curve that reports its curvature.
- A differentiable parametrized curve.
- A parametrized curve that reports its extrema.
- The source curve for curve fitting.
- A parametrized curve that reports the nearest point.
- A generic trait for open and closed shapes.
Functions§
- Convert multiple cubic Bézier curves to quadratic splines.
- Create a new dashing iterator.
- Generate a Bézier path that fits the source curve.
- Generate a highly optimized Bézier path that fits the source curve.
- Fit a single cubic to a range of the source curve.
- Flatten the path, invoking the callback repeatedly.
- Transform an iterator over path elements into one over path segments.
- Expand a stroke into a fill.
Type Aliases§
- Collection of values representing lengths in a dash pattern.